package com.tusuo.stds.ui.rank;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tusuo.stds.R;
import com.tusuo.stds.rest.RankList;
import com.tusuo.stds.util.Utils;

import java.util.List;

import cn.carbs.android.avatarimageview.library.AvatarImageView;

/**
 * @author J.Huang
 * @version 1.0
 *
 */
class RankListAdapter extends BaseAdapter {

    private List<RankList.Rank> dataList;
    private LayoutInflater layoutInflater;
    private Context context;

    RankListAdapter(Context context, List<RankList.Rank> dataList) {
        this.context = context;
        this.dataList = dataList;
        this.layoutInflater = LayoutInflater.from(context);
    }

    private final class Component {
        ImageView ivRank;
        TextView tvRank;
        AvatarImageView aiv;
        TextView tvName;
        TextView tvSchoolName;
        TextView tvScore;
        TextView tvTime;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public RankList.Rank getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Component component = null;
        if (convertView == null) {
            component = new Component();
            // 获得组件，实例化组件
            convertView = layoutInflater.inflate(R.layout.rank_list_item, null);

            component.ivRank = (ImageView) convertView.findViewById(R.id.rank_list_item_iv_rank);
            component.tvRank = (TextView) convertView.findViewById(R.id.rank_list_item_tv_rank);
            component.aiv = (AvatarImageView) convertView.findViewById(R.id.rank_list_item_aiv_avatar);
            component.tvName = (TextView) convertView.findViewById(R.id.rank_list_item_tv_name);
            component.tvSchoolName = (TextView) convertView.findViewById(R.id.rank_list_item_tv_school);
            component.tvScore = (TextView) convertView.findViewById(R.id.rank_list_item_tv_score);
            component.tvTime = (TextView) convertView.findViewById(R.id.rank_list_item_tv_time);

            convertView.setTag(component);
        } else {
            component = (Component) convertView.getTag();
        }

        RankList.Rank rank = dataList.get(position);

        // 绑定数据

        switch (rank.Rank) {
            case 1:
                component.ivRank.setVisibility(View.VISIBLE);
                component.ivRank.setImageResource(R.drawable.rank_ic_1);
                component.tvRank.setVisibility(View.GONE);
                break;
            case 2:
                component.ivRank.setVisibility(View.VISIBLE);
                component.ivRank.setImageResource(R.drawable.rank_ic_2);
                component.tvRank.setVisibility(View.GONE);
                break;
            case 3:
                component.ivRank.setVisibility(View.VISIBLE);
                component.ivRank.setImageResource(R.drawable.rank_ic_3);
                component.tvRank.setVisibility(View.GONE);
                break;
            default:
                component.ivRank.setVisibility(View.GONE);
                component.tvRank.setVisibility(View.VISIBLE);
                component.tvRank.setText(String.valueOf(rank.Rank));
                break;
        }

        if (TextUtils.isEmpty(rank.Photo)) {
            component.aiv.setImageResource(R.drawable.img_avatar_default);
        } else {
            Glide
                    .with(context)
                    .load(Utils.getCorrectImageUrl(rank.Photo))
                    .placeholder(R.drawable.img_avatar_default)
                    .centerCrop()
                    .crossFade()
                    .into(component.aiv);
        }

        int hour = rank.ClearanceTime / 3600;
        int minute = rank.ClearanceTime % 3600 / 60;
        int second = rank.ClearanceTime % 60;

        String hourStr = (hour < 1) ? "" : hour + ":";
        String minuteStr = (minute < 10) ? "0" + minute : "" + minute;
        String secondStr = (second < 10) ? "0" + second : "" + second;

        String sumTime = hourStr + minuteStr + "'" + secondStr + "\"";
        component.tvTime.setText(sumTime);

        if (TextUtils.isEmpty(rank.SchoolName)) {
            component.tvSchoolName.setText("神秘学校");
        }else {
            component.tvSchoolName.setText(rank.SchoolName);
        }

        component.tvScore.setText(String.valueOf(rank.Score));

        String name = Utils.Text.getTruncatedString(rank.Name, 16);
        component.tvName.setText(name);

        return convertView;
    }
}
